import 'package:backend.auth/domain/entities/access_token.dart';
import 'package:backend.core/core/domain/entities/access_token_claim.dart';
import 'package:backend.core/core/domain/services/access_token_service/service.dart';

abstract class IssueAccessToken {
  AccessToken call({
    required AccessTokenClaim accessTokenClaim,
  });
}

class IssueAccessTokenImpl implements IssueAccessToken {
  const IssueAccessTokenImpl({
    required AccessTokenService accessTokenService,
  }) : _accessTokenService = accessTokenService;

  final AccessTokenService _accessTokenService;

  @override
  AccessToken call({
    required AccessTokenClaim accessTokenClaim,
  }) {
    final currentTime = DateTime.now().toUtc();
    final expirationTime = currentTime.add(const Duration(hours: 6));

    final accessTokenData = _accessTokenService.sign(
      accessTokenClaim: accessTokenClaim,
      expirationTime: expirationTime,
    );

    final accessToken = AccessToken(
      claim: accessTokenClaim,
      expirationTime: expirationTime,
      data: accessTokenData,
    );

    return accessToken;
  }
}
